﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using Web.Framework.Translate;
using Web.Framework;
using CrmContacto.Contracts.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.WebApp.Code.InnerClasses;
using CrmContacto.Contracts.Enums;
using CrmContacto.Contracts;

namespace CrmContacto.WebApp.App
{
    public partial class PaisList : BasePage
    {
        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton nuevaPaisnButton = new LinkButton();
            nuevaPaisnButton.ID = "BoListaDistribucionActionMenuNuevoPtoVta";
            nuevaPaisnButton.Text = LocalizationProvider.Instance["Bo.Pais.ActionMenu.Nuevo"];
            nuevaPaisnButton.Click += new EventHandler(nuevPaisButton_Click);
            actionButtons.Add(nuevaPaisnButton);
            //nuevaPaisnButton.Visible = base.NavigationContext != null && !base.NavigationContext.AgregandoPuntosDeVentaAAccionDeMarketing;            

            base.LoadContextActions(actionButtons);
        }

        private void nuevPaisButton_Click(object sender, EventArgs e)
        {
            AgregarNuevoPais();
        }               

        private void SetThisPageToGoBack()
        {
            this.NavigationContext.UrlToGoBack = Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPaisList;
        }

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.lnkBuscar.Attributes.Add("aspnetid", this.lnkBuscar.ClientID);

                // HasPermissionToSeeMe(PlanProdSecurity.DESTINO_VER);
                if (!this.IsPostBack)
                {
                    this.LoadCbos();
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void LoadCbos()
        {
            //UIHelper.LoadEstados<EstadoListaDeDistribucion>(this.cboEstadoListaDistribucion, LocalizationProvider.Instance["DropDown.Todos"]);
        }

        protected override void ApplySecurity()
        {
            //this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            //if (this.Grid.DataSource != null)
            //{
            //    this.Grid.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            //    this.Grid.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            //}
        }

        private void Buscar()
        {
            PaisSearch search = new PaisSearch();
            search.LoadProvincia = false;
            search.NombreLike = this.txtNombre.Text;

            //WebServiceHelper.GetPuntosDeVenta();
            List<Pais> paisList = WebServiceHelper.GetPaisesDto(search);

            this.Grid.DataSource = paisList;
            this.Grid.DataBind();

            pnlResults.Visible = true;
        }        

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Pais pais = (Pais)e.Row.DataItem;

                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    e.Row.Cells[1].Attributes.Add("title", "Eliminar");
                    ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    SetJavascriptForDeleteRecord(btnEliminarCtrl, pais.Nombre);                      
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int paisId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    //SetThisPageToGoBack();
                    this.Response.Redirect(string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPaisEdit + "?Id={0}", paisId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int paisId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarPaisDto(paisId);
                    Buscar();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void AgregarNuevoPais()
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlPaisEdit, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.Grid.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}